-- just simple treasure

local assets = {
	Asset("ANIM", "anim/musha_marks_spot.zip")
}

local prefabs = {
	"collapse_small"
}

local function dig_up(inst, chopper)
	local chest = SpawnPrefab("treasurechest")
	chest.Transform:SetPosition(inst:GetPosition():Get())
	if chest.components.container then
		chest.components.container:GiveItem(SpawnPrefab("musha_yamche_egg"))
		chest.components.container:GiveItem(SpawnPrefab("musha_glowdust"))
		chest.components.container:GiveItem(SpawnPrefab("musha_glowdust"))
		chest.components.container:GiveItem(SpawnPrefab("musha_glowdust"))
		chest.components.container:GiveItem(SpawnPrefab("musha_glowdust"))
		chest.components.container:GiveItem(SpawnPrefab("flowerhat"))
		chest.components.container:GiveItem(SpawnPrefab("backpack_blueprint"))
		chest.components.container:GiveItem(SpawnPrefab("hammer"))
		chest.components.container:GiveItem(SpawnPrefab("axe"))
		chest.components.container:GiveItem(SpawnPrefab("pickaxe"))
		chest.components.container:GiveItem(SpawnPrefab("goldnugget"))
		chest.components.container:GiveItem(SpawnPrefab("goldnugget"))
	end

	SpawnPrefab("small_puff").Transform:SetPosition(inst:GetPosition():Get())
	SpawnPrefab("collapse_small").Transform:SetPosition(inst:GetPosition():Get())

	inst:Remove()
end

local function fn(Sim)
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	inst:AddTag("musha_treasure")

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon("musha_yamche_egg.tex")
	--inst.MiniMapEntity:SetPriority(5)
	inst.MiniMapEntity:SetDrawOverFogOfWar(true)

	inst.AnimState:SetBank("musha_marks_spot")
	inst.AnimState:SetBuild("musha_marks_spot")
	inst.AnimState:PlayAnimation("anim")

	inst.entity:SetPristine()
	if not TheWorld.ismastersim then
		return inst
	end

	inst:AddComponent("inspectable")

	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.DIG)
	inst.components.workable:SetOnFinishCallback(dig_up)
	inst.components.workable:SetWorkLeft(1)

	SpawnPrefab("small_puff").Transform:SetPosition(inst:GetPosition():Get())
	SpawnPrefab("shovel").Transform:SetPosition(inst:GetPosition():Get())

	--[[local x, y, z = inst.Transform:GetLocalPosition()
    	local minimap = GetWorld().minimap.MiniMap
    	local map = GetWorld().Map
        local cx, cy, cz = map:GetTileCenterPoint(x, 0, z)
        minimap:ShowArea(cx, cy, cz, 20)
        map:VisitTile(map:GetTileCoordsAtPoint(cx, cy, cz))
		inst.MiniMapEntity:SetDrawOverFogOfWar(true)
		local px, py, pz = GetPlayer().Transform:GetWorldPosition()
    	GetPlayer().HUD.controls:ToggleMap()
    	minimap:Focus(x - px, z - pz, -minimap:GetZoom()) --Zoom in all the way	
		]]
	return inst
end

return Prefab("musha_treasure_first", fn, assets, prefabs)
